<?php

require_once 'classes/LightPressPlugin.php';

class TagMetaKeywords extends LightPressPlugin {

    var $default_context = 24; // LP_CONTEXT_POST | LP_CONTEXT_STATIC
    var $active = true;
    var $hooks = array('parse_post');

    var $description = 'Adds a meta="keywords" block to the page header containing all tags from the current view.';
    var $constructor_args = array(
        'always_include' =>     'Keywords to always include in the page\'s "meta=keywords" header value',
        'include_categories' => 'Set to 1 to include post categories.'
        );
    
    var $_frontend;
	
    // options
    var $always_include = '';
    var $include_categories = 1;

    function TagMetaKeywords(&$frontend, $args, $dummy_run=false) {
		$this->LightPressPlugin($frontend, $args, $dummy_run);
	}
 
    function run($hook, &$post) {
        if (!$this->active)
            return ($this->hide());
        
        $f =& $this->_frontend; 
		$tpl =& $f->tpl;
        
        // add keywords & categories to page keywords array
        $pagekeys = explode(',', $this->always_include);
        
        
        if (isset($post['tags']) && count($post['tags']) > 0) {
            $pagekeys = array_merge($pagekeys, $post['tags']);
        }
        
        if ($this->include_categories && isset($post['categories'])) {
            $categories =& $f->getCategories();
            foreach ($post['categories'] as $post_category)
                $pagekeys[] = $categories[$post_category]['cat_name'];
        }
        $pagekeys = array_unique($pagekeys);
        
        // setup meta keywords for page header
        $keywordlist = array();
        foreach($pagekeys as $keyword) {
            $keyword = trim($keyword);
            if ($keyword)
                $keywordlist[] = htmlspecialchars($keyword); 
        }
        $tpl->setVar('PLUGIN_TAGMETAKEYWORDS', '<meta name="keywords" content="' . implode(', ', $keywordlist) . '" />');	
	}
    
	function hide() {
        $this->_frontend->tpl->setVar('PLUGIN_TAGMETAKEYWORDS', '');
    }
}

?>
